package com.github.prontera.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.Issue;
import com.github.prontera.domain.IssueAnswer;
import com.github.prontera.domain.enums.YesOrNoEnum;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.IssueAnswerService;
import com.github.prontera.service.IssueService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 问答
 */
@RestController
@Api(value = "问答")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/issue")
public class IssueController{

	@Autowired
	private IssueService issueService;
	@Autowired
	private IssueAnswerService issueAnswerService;
	
	
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户红包提问",notes = "用户红包提问")
	public ObjectDataResponse<Issue> insert(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Issue issue){
		if(issue.getIssue()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"issue必传!");
		}
		if(issue.getAmount()==null||issue.getAmount().doubleValue()<0.01){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"amount必传!");
		}
		if(issue.getAcceptorId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"acceptorId必传!");
		}
		issue.setClientId(clientId);
		issue.setHasPaySuccess(YesOrNoEnum.NO);//默认是支付失败, 这样前台就不展示这条数据
		issueService.insert(issue);
		return new ObjectDataResponse<Issue>(issue);
	}
	
	
	
	@RequestMapping(value = "updateShareCount", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新视频分享次数",notes = "更新视频分享次数")
	public ObjectDataResponse<Boolean> updateShareCount(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody IssueAnswer issueAnswer){
		if(issueAnswer.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"id必传!");
		}
		issueAnswerService.updateShareCount(issueAnswer.getId());
		return new ObjectDataResponse<Boolean>(true);
	}
	
	
	@RequestMapping(value = "updateThumbsUpCount", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "更新视频点赞次数",notes = "更新视频点赞次数")
	public ObjectDataResponse<Boolean> updateThumbsUpCount(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody IssueAnswer issueAnswer){
		if(issueAnswer.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION,"id必传!");
		}
		issueAnswer.setClientId(clientId);
		issueAnswerService.updateThumbsUpCount(issueAnswer);
		return new ObjectDataResponse<Boolean>(true);
	}
	
	
}
